﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web.Mvc;
using InfoTech.Core.Service;
using InfoTech.LanTranh.MucVu.Bussiness;

namespace InfoTech.LanTranh.MucVu.Service
{
    public class GIAOHOService : BaseService
    {
        public static bool Check_Insert_GIAOHO(GIAOHO giaoho)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<GIAOHO>().FirstOrDefault(c => c.TENGIAOHO.ToUpper() == giaoho.TENGIAOHO.ToUpper()
                            && c.MAGIAOXU == giaoho.MAGIAOXU) == null;
            result &= dataContext.GetTable<GIAOHO>().FirstOrDefault(c => c.MAHOID.ToUpper() == giaoho.MAHOID.ToUpper()
                            && c.MAGIAOXU == giaoho.MAGIAOXU) == null;
            return result;
        }

        public static bool Check_Update_GIAOHO(GIAOHO giaoho)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<GIAOHO>().FirstOrDefault(c => c.TENGIAOHO.ToUpper() == giaoho.TENGIAOHO.ToUpper()
                            && c.MAGIAOXU == giaoho.MAGIAOXU && c.MAGIAOHO != giaoho.MAGIAOHO) == null;
            result &= dataContext.GetTable<GIAOHO>().FirstOrDefault(c => c.MAHOID.ToUpper() == giaoho.MAHOID.ToUpper()
                            && c.MAGIAOXU == giaoho.MAGIAOXU && c.MAGIAOHO != giaoho.MAGIAOHO) == null;
            return result;
        }

        public static bool Check_Delete_GIAOHO(string[] strings)
        {
            return true;
        }

        public static IEnumerable<SelectListItem> Get_List_Join_GiaoPhan_GiaoHat_GiaoXu()
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Giáo Họ} ---", Value = "" });
            list.AddRange(new SelectList(dataContext.GetTable<GIAOHAT>()
                .Join(dataContext.GetTable<GIAOPHAN>(), c => c.MAGIAOPHAN, v => v.MAGIAOPHAN,
                    (c, v) => new { c.MAGIAOHAT, TENGIAOHAT = c.TENGIAOHAT + " - " + v.TENGIAOPHAN })
                .Join(dataContext.GetTable<GIAOXU>(), c => c.MAGIAOHAT, v => v.MAGIAOHAT,
                    (c, v) => new { v.MAGIAOXU, TENGIAOXU = v.TENGIAOXU + " - " + c.TENGIAOHAT })
                    .Join(dataContext.GetTable<GIAOHO>(), c => c.MAGIAOXU, v => v.MAGIAOXU,
                    (c, v) => new { v.MAGIAOHO, TENGIAOHO = v.TENGIAOHO + " - " + c.TENGIAOXU })
                    .OrderBy(c => c.TENGIAOHO)
                , "MAGIAOHO", "TENGIAOHO"));
            return list;
        }

        public static IEnumerable<SelectListItem> Get_List_Join_GiaoPhan_GiaoHat_GiaoXu_Data()
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Giáo Họ} ---", Value = "" });
            list.AddRange(dataContext.GetTable<GIAOHAT>().Where(c => c.ISDATA ?? false)
                .Join(dataContext.GetTable<GIAOPHAN>().Where(c => c.ISDATA ?? false),
                      c => c.MAGIAOPHAN, v => v.MAGIAOPHAN,
                      (c, v) => new { c.MAGIAOHAT, TENGIAOHAT = c.TENGIAOHAT + " - " + v.TENGIAOPHAN })
                .Join(dataContext.GetTable<GIAOXU>().Where(c => c.ISDATA ?? false),
                      c => c.MAGIAOHAT, v => v.MAGIAOHAT,
                      (c, v) => new { v.MAGIAOXU, TENGIAOXU = v.TENGIAOXU + " - " + c.TENGIAOHAT })
                .Join(dataContext.GetTable<GIAOHO>().Where(c => c.ISDATA ?? false),
                      c => c.MAGIAOXU, v => v.MAGIAOXU,
                      (c, v) => new { v.MAGIAOHO, TENGIAOHO = v.TENGIAOHO + " - " + c.TENGIAOXU, v.ISDEFAULT })
                .OrderBy(c => c.TENGIAOHO).
                Select(
                    c =>
                    new SelectListItem() { Text = c.TENGIAOHO, Value = c.MAGIAOHO + "", Selected = c.ISDEFAULT ?? false }));
            return list;
        }

        public static IEnumerable<SelectListItem> Get_List_Join_GiaoPhan_GiaoHat_GiaoXu(string id)
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Giáo Họ} ---", Value = "" });
            list.AddRange(new SelectList(dataContext.GetTable<GIAOHAT>()
                .Join(dataContext.GetTable<GIAOPHAN>(), c => c.MAGIAOPHAN, v => v.MAGIAOPHAN,
                    (c, v) => new { c.MAGIAOHAT })
                .Join(dataContext.GetTable<GIAOXU>().Where(c => c.MAGIAOXU + "" == id), c => c.MAGIAOHAT, v => v.MAGIAOHAT,
                    (c, v) => new { v.MAGIAOXU })
                    .Join(dataContext.GetTable<GIAOHO>(), c => c.MAGIAOXU, v => v.MAGIAOXU,
                    (c, v) => new { v.MAGIAOHO, v.TENGIAOHO })
                    .OrderBy(c => c.TENGIAOHO)
                , "MAGIAOHO", "TENGIAOHO"));
            return list;
        }
    }
}
